package com.imweibokong.platform.service.impl;

import java.util.ArrayList;
import java.util.List;

import weibo4j.DirectMessage;
import weibo4j.Weibo;
import weibo4j.WeiboException;

import com.imweibokong.platform.convertor.WeiBoMessageConvertor;
import com.imweibokong.platform.dataobject.WeiBoMessage;
import com.imweibokong.platform.service.WeiBoService;

public class SinaWeiBoService implements WeiBoService {

    private String userName;
    private String password;
    private Weibo  sinaWeibo;

    public void init() {
        sinaWeibo = new Weibo(userName, password);
    }

    public List<WeiBoMessage> getDirectMessages() {
        try {
            List<DirectMessage> msgs = sinaWeibo.getDirectMessages();
            List<WeiBoMessage> myMessage = new ArrayList<WeiBoMessage>(msgs.size());
            for (DirectMessage sinaMsg : msgs) {
                myMessage.add(WeiBoMessageConvertor.fromSinaMessage(sinaMsg));
            }
            return myMessage;
        } catch (WeiboException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<WeiBoMessage> getDirectMessages(int count) {
        try {
            List<DirectMessage> msgs = sinaWeibo.getDirectMessages();
            List<WeiBoMessage> myMessage = new ArrayList<WeiBoMessage>(msgs.size());
            for (DirectMessage sinaMsg : msgs) {
                myMessage.add(WeiBoMessageConvertor.fromSinaMessage(sinaMsg));
            }
            return myMessage;
        } catch (WeiboException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserName() {
        return userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

}
